package org.utils.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Manuel EVENO
 * @version 1.0
 */
public class DateUtils
{
  private DateUtils()
  {
  }

  public static final String DEFAULT_FORMAT = "dd/MM/yy HH:mm";

  /**
   * Returns a new String representing the current date with the specified format
   *
   * @param format The format to return
   */
  public static String newFormatedDate()
  {
    return formatDate(new Date(), DEFAULT_FORMAT);
  }
  /**
   * Returns a new String representing the current date with the specified format
   *
   * @param format The format to return
   */
  public static String newFormatedDate(String format)
  {
    return formatDate(new Date(), format);
  }
  /**
   * Returns the date in String with the specified format
   *
   * @param d The date to format
   * @param format The format to apply
   */
  public static String formatDate(long d, String format)
  {
  	return formatDate(new Date(d), format);
  }
  /**
   * Returns the date in String with the specified format
   *
   * @param d The date to format
   * @param format The format to apply
   */
  public static String formatDate(Date d, String format)
  {
  	if (d == null)
  		return "";
    SimpleDateFormat formater = new SimpleDateFormat(format);
    formater.setLenient(false);
    return formater.format(d);
  }
  /**
   * Returns the date read from the String using the specified format
   *
   * @param d The date to analyse
   * @param format The format of the date
   */
  public static Date parseDate(String d, String format)
  {
    return parseDate(d, format, false);
  }
  /**
   * Returns the date read from the String using the specified format
   *
   * @param d The date to analyse
   * @param format The format of the date
   * @param lenient Sets if the date parser must be lenient or not
   */
  public static Date parseDate(String d, String format, boolean lenient)
  {
    try
    {
      if (d == null || d.length() == 0)
      	return null;
      SimpleDateFormat formater = new SimpleDateFormat(format);
      formater.setLenient(lenient);
      return formater.parse(d);
    }
    catch (ParseException e)
    {
      throw new DateFormatException(format, d, e.getMessage());
    }
  }
  /**
   * Sets the date to midnight
   *
   * @param d the date
   */
  public static Date dateToMidnight(Date d)
  {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return dateToMidnight(cal);
  }
  /**
   * Sets the Calendar to midnight
   *
   * @param d the date
   */
  public static Date dateToMidnight(Calendar cal)
  {
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.HOUR_OF_DAY, Calendar.AM);
    return cal.getTime();
  }
  public static void getFirstMonthDay() throws java.io.IOException
  {
    SimpleDateFormat formater = new SimpleDateFormat("E dd MMMM yyyy");
    java.io.BufferedReader breader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
    String line = null;
    while ((line = breader.readLine()) != null)
    {
      System.out.println("Enter the month number (1-12): ");
      int num = Integer.parseInt( line ) - 1;
      Calendar cal = new GregorianCalendar();
      cal.set(Calendar.DATE, 1);
      cal.set(Calendar.MONTH, num);
      cal.set(Calendar.YEAR, 2002);
      System.out.println( formater.format(cal.getTime()) );
      if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)
	System.out.println("This is monday");
      else
      {
	cal.clear(Calendar.DAY_OF_WEEK);
	cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
	System.out.println("FirstMonday: "+formater.format(cal.getTime()) );
      }
    }
    breader.close();
  }
}